import { createStore } from 'vuex'
import menus from './modules/menus'

// Create a new store instance.
const store = createStore({
  state () {
    return {
      token: ''
    }
  },
  mutations: {
    // 保存token
    setToken(state, token) {
      sessionStorage.setItem('token', token)
      state.token = token
    },
    // 清除token
    clearToken(state) {
      sessionStorage.removeItem('token')
      state.token = ''
    }
  },
  actions: {
    setToken(context, token) {
      return new Promise((resolve) => {
        context.commit('setToken', token)
        resolve(token)
      })
    }
  },
  modules: {
    menus
  },
  getters: {
    getToken(state) {
      return state.token || sessionStorage.getItem('token')
    }
  }
})

export default store